﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JMT.Data
{
    /// <summary>
    /// 元数据定义
    /// </summary>
    public static class JMetadata
    {
        private static Dictionary<string, Type> _typeMapping;

        /// <summary>
        /// 模型名称和类型的对应关系
        /// </summary>
        public static Dictionary<string, Type> TypeMapping
        {
            get
            {
                if (_typeMapping == null)
                {
                    _typeMapping = new Dictionary<string, Type>();
                    Init(_typeMapping);
                }
                return _typeMapping;
            }
        }

        private static void Init(Dictionary<string, Type> typeMapping)
        {
            _typeMapping.Add(JConstString.TSolution, typeof(JSolution));
            _typeMapping.Add(JConstString.TPackage, typeof(JPackage));
            _typeMapping.Add(JConstString.TDiagram, typeof(JDiagram));
            _typeMapping.Add(JConstString.TClass, typeof(JClass));
            _typeMapping.Add(JConstString.TInterface, typeof(JInterface));
            _typeMapping.Add(JConstString.TProperty, typeof(JProperty));
            _typeMapping.Add(JConstString.TMethod, typeof(JMethod));
            _typeMapping.Add(JConstString.TElement, typeof(JElement));
            _typeMapping.Add(JConstString.TGeneralization, typeof(JGeneralization));
            _typeMapping.Add(JConstString.TAssociation, typeof(JAssociation));
            _typeMapping.Add(JConstString.TAggregation, typeof(JAggregation));
            _typeMapping.Add(JConstString.TComposition, typeof(JComposition));
        }

        public static string GetModelName(Type type)
        {
            foreach (var key in TypeMapping.Keys)
            {
                if (TypeMapping[key] == type)
                {
                    return key;
                }
            }
            return string.Empty;
        }

        public static Type GetModelType(string name)
        {
            foreach (var key in TypeMapping.Keys)
            {
                if (key == name)
                {
                    return TypeMapping[key];
                }
            }
            return null;
        }
    }
}
